package com.ufcg.lvc.server;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ufcg.lvc.client.GreetingService;
import com.ufcg.lvc.client.my.RandomString;
/**
 * 
 * The server side implementation of the RPC service.
 * 
 * @author Larissa Costa
 * 
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	
	@Override
	public boolean gravarCaminhosTodasFotos(String titulo, ArrayList<String> fotos) {
		File dir1 = new File("Fotos");
		String t = titulo + ".txt";
		File arq = new File(dir1, t);
		if (!dir1.exists()) {
			dir1.mkdir();
		}
		try {
			arq.createNewFile();
			FileWriter fileWriter = new FileWriter(arq, false);
			PrintWriter printWriter = new PrintWriter(fileWriter);
			for (String foto : fotos) {
				printWriter.println("1 " + foto);	
			}
			printWriter.flush();
			printWriter.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean gravarCaminhosFotosPagina(ArrayList<String> fotos) {
		File dir1 = new File("Fotos");
		String t = "ArquiFotoPag.txt";
		File arq = new File(dir1, t);
		if (!dir1.exists()) {
			dir1.mkdir();
		}
		try {
			arq.createNewFile();
			FileWriter fileWriter = new FileWriter(arq, false);
			PrintWriter printWriter = new PrintWriter(fileWriter);
			for (String foto : fotos) {
				printWriter.println("1 " + foto);	
			}
			printWriter.flush();
			printWriter.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	
	@Override
	public ArrayList<String> gerarDiagramacao(int quantidade, int largura, int altura) {
		Runtime r = Runtime.getRuntime();
		ArrayList<String> saida = new ArrayList<String>();
		try {
			Process p = r.exec("C:\\executavelDAF\\daf.exe C:\\Users\\larissa\\workspaceNOV\\Foto__\\war\\Fotos\\ArquiFotoPag.txt "+largura+" "+altura);
			InputStream in = p.getInputStream();
			BufferedInputStream buf = new BufferedInputStream(in);
			InputStreamReader inread = new InputStreamReader(buf);
			BufferedReader bufferedreader = new BufferedReader(inread);
			
			// Read the ls output
			String line = "--";
			int i = 0;
			while (i < quantidade) {
				line = bufferedreader.readLine();
				saida.add(line);
//				System.out.println(line + " resultado diagramacao");
				i++;
			}
			// Check for ls failure
			bufferedreader.close();
			inread.close();
			buf.close();
			in.close();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		return saida;
	}
	
	@Override
	public ArrayList<ArrayList<String>> gerarThumbnails(ArrayList<String> caminhoCompletoFotos) {
		Runtime r = Runtime.getRuntime();
		ArrayList<ArrayList<String>> saida = new ArrayList<ArrayList<String>>();
		ArrayList<String> nomes = new ArrayList<String>();
		ArrayList<String> caminhos = new ArrayList<String>();
		
		try {
			for (int i = 0; i < caminhoCompletoFotos.size(); i++) {

				String oldPath= caminhoCompletoFotos.get(i);
				String newPath= caminhoCompletoFotos.get(i).replace("Foto__\\war\\Fotos\\", "Foto__\\war\\Thumb\\");
				String comando = "C:\\progra~1\\ImageMagick-6.8.5-Q16\\convert.exe " + oldPath +" -resize x130 " + newPath;
//				String comando = "cmd /c start /wait /MIN convert "+ oldPath +" -resize x150 "+ newPath;
				r.exec(comando);				
				nomes.add("Thumb"+newPath.split("Thumb")[1]);
				caminhos.add(newPath);
			}
			saida.add(nomes);
			saida.add(caminhos);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		return saida;
	}
	
	@Override
	public ArrayList<String> saveImageURL(ArrayList<String> listaURL) throws MalformedURLException, IOException {
		RandomString r = new RandomString(20);
		ArrayList<String> caminhos = new ArrayList<String>();
		for (String imageUrl : listaURL) {
			String newName = r.nextString() + "1" + imageUrl.substring(imageUrl.length() - 4);
			FileUtils.copyURLToFile(new URL(imageUrl), new File("Fotos/"+newName));
			FileUtils.copyURLToFile(new URL(imageUrl.replace("_n.", "_s.")), new File("Thumb/"+newName));
			caminhos.add("C:\\Users\\larissa\\workspaceNOV\\Foto__\\war\\Fotos\\"+newName);
		}		
		return caminhos;
	}
	
}